<?php
$loader->requireOnce('includes/ReportException.class.php');
/**
 * Report button bar generation
 *  
 * @author Pham Trung Nguyen
 *
 */
class ReportButton {

    const BUTTON_VIEW     = 'button_view';
    const BUTTON_PRINT    = 'button_print';
    const BUTTON_EXPORT   = 'button_export';
    
    const EXPORT_XML      = 'xml';
    const EXPORT_EXCEL    = 'excel';
    const EXPORT_PDF      = 'pdf';
    
    /**
     * 
     * @var Controller
     */
    protected $controller   = NULL;
    protected $reportName   = NULL;
    protected $logger       = NULL;
        
    protected $buttonArray  = array();
    protected $exportArray  = array();
    
    /**
     * For validation     
     */
    private $exportType   = array(
            self::EXPORT_EXCEL, 
            self::EXPORT_PDF, 
            self::EXPORT_XML);
            
    private $buttonType = array(
            self::BUTTON_VIEW,
            self::BUTTON_PRINT,
            self::BUTTON_EXPORT
        );    
    
    /**
     * Constructor
     * @param $controller @see Controller
     * @param $reportName internal name of report @see ReportConstant
     */
    public function __construct($controller, $reportName) {
        $this->logger = LoggerManager::getLogger('modules.report.includes.ReportButton');
        
        if (!($controller instanceof Controller)) {            
            throw new ReportException("The object $controller is not instance of Controller");
        }                        
        $this->controller = $controller;
        $this->reportName = $reportName;  
    }
    
    /**
     * 
     * @param String $type	the button type 
     * @param Array btnParam in case of button type is BUTTON_EXPORT
     * 			the btnParam contains the export file type
     * 			btnParam = array(EXPORT_EXCEL, EXPORT_PDF);
     * @throws
     * 		ReportException if the $type is invalid
     */
    public function addButton($type, $btnParam = array()) {
        if (!in_array($type, $this->buttonType)) {            
            throw new ReportException("The button type '$type' is invalid");    
        }
        $this->buttonArray[] = $type;
        
        if ($type == self::BUTTON_EXPORT) {
            foreach ($btnParam as $exportItem) {
                if (!in_array($exportItem, $this->exportType)) {                    
                    throw new ReportException("The export type '$exportItem' is invalid");
                }
                $this->exportArray[] = $exportItem;
            }
        }        
    }
    
    /**
     * 
     * @return HTML Button bar
     * 		
     */
    public function renderButton() {
        $result = '';
        $currentControllerPath = $this->controller->view->path;
        $this->controller->view->path = 'report_button';    
        
        $this->controller->assign('reportName', $this->reportName);
        $this->controller->assign('buttonArray', $this->buttonArray);
        $this->controller->assign('exportArray', $this->exportArray);                
        $result .= $this->controller->view->render('button.html');
        
        $this->controller->view->path = $currentControllerPath;             
        return $result;              
    }           
}